package com.aishua.fire.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import com.aishua.fire.entity.ateampteam.UpdateAteamPteamInfo;
import com.aishua.fire.jpa.AccountteamPermteamDao;
import com.aishua.fire.jpa.entity.Accountteam;
import com.aishua.fire.jpa.entity.AccountteamPermteam;
import com.aishua.fire.jpa.entity.AccountteamPermteamId;
import com.aishua.fire.mapper.AccountteamPermteamMapper;
import com.aishua.fire.mapper.AteamPermteamMapper;
import com.aishua.fire.mapper.dto.AccountteamPermteamDto;
import com.aishua.fire.mapper.dto.AteamPermteamDto;
import com.aishua.fire.service.AccountteamPermteamService;
import com.aishua.fire.service.AccountteamService;

@Service
public class AccountteamPermteamServiceImpl implements AccountteamPermteamService {

	@Autowired
	private AccountteamPermteamDao ateampteamDao;
	@Autowired
	private AccountteamService accountteamService;
	@Autowired
	private AccountteamPermteamMapper accountteamPermteamMapper;
	@Autowired
	private AteamPermteamMapper ateamPermteamMapper;
	
	
	/*@Transactional
	public void add(AddAteamPteamInfo addAteamPteamInfo) {
		AccountteamPermteam ateamPteam = new AccountteamPermteam();
		
		ateamPteam.setAccountteamPermteamId(new AccountteamPermteamId(Long.parseLong(addAteamPteamInfo.getAccountteamId()),Long.parseLong(addAteamPteamInfo.getPermteamId())));
		ateamPteam.setAuthAction(addAteamPteamInfo.getAuth_action());
		ateamPteam.setAuthType(addAteamPteamInfo.getAuth_type());
		ateamPteam.setCreateDate(new Date());
		ateampteamDao.save(ateamPteam);

	}*/
	@Override
	public void add(AteamPermteamDto ateamPermteamDto) {
		
		ateamPermteamMapper.addAteamPermteam(ateamPermteamDto);
	}
	
	public void updateateam(AteamPermteamDto ateamPermteamDto) {
		
		ateamPermteamMapper.updateateam(ateamPermteamDto);
	}
	
	public void delete(AteamPermteamDto ateamPermteamDto) {
		
		ateamPermteamMapper.delete(ateamPermteamDto);
	}
	
	@Override
	public void update(UpdateAteamPteamInfo updateAteamPteamInfo) {
		AccountteamPermteam ateamPteam = new AccountteamPermteam();
		
		ateamPteam.setAccountteamPermteamId(new AccountteamPermteamId(Long.parseLong(updateAteamPteamInfo.getAccountteamId()),Long.parseLong(updateAteamPteamInfo.getPermteamId())));
		ateamPteam.setAuthAction(updateAteamPteamInfo.getAuth_action());
		ateamPteam.setAuthType(updateAteamPteamInfo.getAuth_type());
		ateampteamDao.saveAndFlush(ateamPteam);
	}

	@Override
	public AccountteamPermteam findDetailByPK(String ateamId, String pteamId) {
		AccountteamPermteamId id = new AccountteamPermteamId(Long.parseLong(ateamId), Long.parseLong(pteamId));
		return ateampteamDao.findOne(id);
	}

	@Override
	public Page<AccountteamPermteam> searchList(int page, int size,final String authAction,final String authType) {
		Pageable pageable = (Pageable) new PageRequest(page, size, new Sort("authType"));
		return ateampteamDao.findAll(new Specification<AccountteamPermteam>() {
            
			@Override
            public Predicate toPredicate(Root<AccountteamPermteam> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> authActionPath = root.get("authAction");
                Path<String> authTypePath = root.get("authType");
                //连接查询条件, 不定参数，可以连接0..N个查询条件
                query.where(cb.like(authActionPath, "%" + authAction + "%"),cb.like(authTypePath, "%" + authType + "%")); //这里可以设置任意条查询条件
                return null;
            }
        },pageable);
	}
	//全查不分页
	@Override
	public List<AccountteamPermteam> searchList() {
		return ateampteamDao.findAll();
	}
	//根据accountteamId查询
	public List<Object> findByAccountteamId(Long accountteamId) {
		List<Object> accountteamPermteams = ateampteamDao.findByAccountteamId(accountteamId);
		return accountteamPermteams;
	}
		
	//根据accountteamId查询账户组-权限组表所有信息map
	public Map<String, Object> findAllByAccountteamId(Long accountteamId) {
		Map<String, Object> map=new HashMap<>();
		//调用根据账户组id查询accountteamPermteam
		List<Object> accountteamPermteams = findByAccountteamId(accountteamId);
		//根据账户组id查询账户组信息
		Accountteam accountteam = accountteamService.findRoleId(accountteamId+"");
		//通过账户id获取账户组列表信息
		map.put("accountteam", accountteam);
		map.put("accountteamPermteams", accountteamPermteams);
		return map;
	}

	@Override
	public List<AccountteamPermteamDto> getAccteamPermteamInfo(String permteamId){
		
		return accountteamPermteamMapper.getAccountteamPermteamInfo(Long.parseLong(permteamId));
	}

}
